package com.mlh.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mlh.entity.User;
import com.mlh.enums.AppHttpCodeEnum;
import com.mlh.exception.SystemException;
import com.mlh.mapper.UserMapper;
import com.mlh.service.UserService;
import com.mlh.util.BeanCopyUtil;
import com.mlh.util.ResponseResult;
import com.mlh.util.SecurityUtils;
import com.mlh.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-03-15 20:55:52
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //注入我们的加密器  这里加密器是在之前springsecurity向ioc容器中注入的
    @Autowired
   private PasswordEncoder passwordEncoder;

    //获取用户信息
    @Override
    public ResponseResult getUserInfo() {
        Long userId = SecurityUtils.getUserId();
        // LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        // queryWrapper.eq(User::getId,userId);
        User user = getById(userId);
        //获取原生user之后需要转成userVo
        UserInfoVo userInfoVo = BeanCopyUtil.copyBean(user, UserInfoVo.class);
        //返回结果
        return ResponseResult.okResult(userInfoVo);
    }

    //更新用户信息
    @Override
    public ResponseResult updateUserInfo(User user) {
        //这里存在大量的逻辑去优化  不能仅仅简单的调用updateById 就去更新用户信息
        updateById(user);
        return ResponseResult.okResult();
    }

    //用户注册
    @Override
    public ResponseResult userRegister(User user) {
        //用户注册本质就是将用户信息存入到数据库中
        //对数据进行非空判断
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getEmail())){
            throw new SystemException(AppHttpCodeEnum.EMAIL_NOT_NULL);
        }
        if(!StringUtils.hasText(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_NULL);
        }
        //对数据进行是否存在的判断
        if(userNameExist(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
        }
        if(nickNameExist(user.getNickName())){
            throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
        }
        //....
        //接收到密码是明文，我们需要将其加密，在存入数据库中
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        save(user);
        return ResponseResult.okResult();
    }

    //判断用户名称是否和数据库中现有数据产生重复
    private boolean nickNameExist(String nickName) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNickName, nickName);
        return count(queryWrapper)>0;
    }

    //判断用户名是否和数据库中现有数据产生重复
    private boolean userNameExist(String userName) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName, userName);
        return count(queryWrapper)>0;
    }
}
